package com.mmc.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.mmc.entity.Content;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.DeleteListenPoint;
import com.xpand.starter.canal.annotation.InsertListenPoint;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * 作者：李嘉浩
 * 时间：2024-09-16-21:29
 * 功能：
 */

@CanalEventListener
public class ContentListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // @InsertListenPoint(table = {"tb_content"})
    @ListenPoint(eventType = {CanalEntry.EventType.INSERT, CanalEntry.EventType.UPDATE},table = "tb_content")
    public void brandInsert(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //获得插入后的字段
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();

        Content content = new Content();

        for (CanalEntry.Column column : afterColumnsList) {
            String name = column.getName();
            String value = column.getValue();
            if (name.equals("id")){
                content.setId(Long.parseLong(value));
            }
            if (name.equals("category_id")){
                content.setCategoryId(Long.parseLong(value));
            }
            if (name.equals("title")){
                content.setTitle(value);
            }
            if (name.equals("url")){
                content.setUrl(value);
            }
            if (name.equals("pic")){
                content.setPic(value);
            }
            if (name.equals("status")){
                content.setStatus(value);
            }
            if (name.equals("sort_order")){
                content.setSortOrder(Long.parseLong(value));
            }
        }
        redisTemplate.opsForHash().put("content_map",content.getId()+"", JSON.toJSONString(content));
    }


    @DeleteListenPoint(table = {"tb_content"})
    public void brandDelete(CanalEntry.EventType eventType, CanalEntry.RowData rowData){
        //获得插入后的字段
        List<CanalEntry.Column> afterColumnsList = rowData.getBeforeColumnsList();
        Content content = new Content();

        for (CanalEntry.Column column : afterColumnsList) {
            String name = column.getName();
            String value = column.getValue();
            if (name.equals("id")){
                content.setId(Long.parseLong(value));
            }
            if (name.equals("category_id")){
                content.setCategoryId(Long.parseLong(value));
            }
            if (name.equals("title")){
                content.setTitle(value);
            }
            if (name.equals("url")){
                content.setUrl(value);
            }
            if (name.equals("pic")){
                content.setPic(value);
            }
            if (name.equals("status")){
                content.setStatus(value);
            }
            if (name.equals("sort_order")){
                content.setSortOrder(Long.parseLong(value));
            }
        }
        redisTemplate.opsForHash().delete("content_map",content.getId()+"");
    }
}
